<?php
/**
 * Created by PhpStorm.
 * User: lzy
 * Date: 2019/3/1
 * Time: 17:50
 */
namespace App\Ordersys\Controllers\Admin;
use App\Base\Contracts\AreaInterface;
use App\Base\Controllers\Controller;
use App\Base\Models\AreaModel;
use App\Ordersys\Contracts\OrderanalysisInterface;
use App\Ordersys\Models\OrdersysOrderLogModel;
use Illuminate\Http\Request;


class OrderanalysisController extends Controller
{

    /**
     *
     * @return OrdersysOrderLogModel
     */
    public function getOrdersysOrderLogModel(){
        return app()->make(OrdersysOrderLogModel::class);
    }
    /**
     * 统计接口
     * @return OrderanalysisInterface
     */
    public function getOrderanalysisInterface(){
        return app()->make(OrderanalysisInterface::class);
    }

    /**
     * @return AreaModel
     */
    public function getAreaModel(){
        return app()->make(AreaModel::class);
    }

    /**
     * @return AreaInterface
     */
    public function getAreaInterface(){
        return app()->make(AreaInterface::class);
    }


    public function index(Request $request){
        if($request->getMethod() == 'POST'){
            $where=function($query) use ($request){
                $start_date = $request->input("start_date", "");
                if (!empty($start_date)) {
                    $query->where("update_time", ">", strtotime($start_date));
                }
                $end_date = $request->input("end_date", "");
                if (!empty($end_date)) {
                    $query->where("update_time", "<", strtotime($end_date." 23:59:59"));
                }
                $saleName = $request->input("sale_name", "");
                if(!empty($saleName)){
                    $query->where("sale_name","like",'%'.$saleName.'%');
                }

                $query->where("option_type",10);
            };
            $pagelist=$this->getOrderanalysisInterface()->getSaleIndex($where);
            if(!empty($pagelist -> items())){
                foreach($pagelist -> items()  as $k => $v){
                    $where2=function($query) use ($request,$v){
                        $start_date = $request->input("start_date", "");
                        if (!empty($start_date)) {
                            $query->where("update_time", ">", strtotime($start_date));
                        }
                        $end_date = $request->input("end_date", "");
                        if (!empty($end_date)) {
                            $query->where("update_time", "<", strtotime($end_date." 23:59:59"));
                        }
                        $saleName = $request->input("sale_name", "");
                        if(!empty($saleName)){
                            $query->where("sale_name","like",'%'.$saleName.'%');
                        }

                        $query->where("option_type",20);
                        $query->where("sale_id",$v->sale_id);
                    };
                    $v['return_money'] = $this->getOrderanalysisInterface()->getSumSaleMoney($where2);
                    $data[] = $v ;
                    //新粉-->
                    $fenwhere=function($query) use($request,$v){
                        $start_date = $request->input("start_date", "");
                        if (!empty($start_date)) {
                            $query->where("update_time", ">", strtotime($start_date));
                        }
                        $end_date = $request->input("end_date", "");
                        if (!empty($end_date)) {
                            $query->where("update_time", "<", strtotime($end_date." 23:59:59"));
                        }
                        $query->where("option_type",10);
                        $query->where("sale_id",$v->sale_id);
                        $query->where("add_fen_type","新粉");
                    };
                    $v['new_fen'] = $this->getOrderanalysisInterface()->getAddFenType($fenwhere);
                    //老粉-->
                    $oldwhere=function($query) use($request,$v){
                        $start_date = $request->input("start_date", "");
                        if (!empty($start_date)) {
                            $query->where("update_time", ">", strtotime($start_date));
                        }
                        $end_date = $request->input("end_date", "");
                        if (!empty($end_date)) {
                            $query->where("update_time", "<", strtotime($end_date." 23:59:59"));
                        }
                        $query->where("option_type",10);
                        $query->where("sale_id",$v->sale_id);
                        $query->where("add_fen_type","老粉");
                    };
                    $v['old_fen'] = $this->getOrderanalysisInterface()->getAddFenType($oldwhere);
                    //复购-->
                    $buywhere=function($query) use($request,$v){
                        $start_date = $request->input("start_date", "");
                        if (!empty($start_date)) {
                            $query->where("update_time", ">", strtotime($start_date));
                        }
                        $end_date = $request->input("end_date", "");
                        if (!empty($end_date)) {
                            $query->where("update_time", "<", strtotime($end_date." 23:59:59"));
                        }
                        $query->where("option_type",10);
                        $query->where("sale_id",$v->sale_id);
                        $query->where("add_fen_type","复购");
                    };
                    $v['buy_fen'] = $this->getOrderanalysisInterface()->getAddFenType($buywhere);


                    $custom = collect(['data' => $data]) ;
                }
                $custom->merge($pagelist);
            }


//            print_r($pagelist);
//            exit;
            return $pagelist;
        }

        return view("ordersys.orderanalysis.index");
    }

    public function weixin(Request $request)
    {
        //新粉-->

        if ($request->getMethod() == 'POST') {
            $where = function ($query) use ($request) {
                $start_date = $request->input("start_date", "");
                if (!empty($start_date)) {
                    $query->where("update_time", ">", strtotime($start_date));
                }
                $end_date = $request->input("end_date", "");
                if (!empty($end_date)) {
                    $query->where("update_time", "<", strtotime($end_date." 23:59:59"));
                }
                $wxName = $request->input("wx_name", "");
                if (!empty($wxName)) {
                    $query->where("wx_name", "like", '%' . $wxName . '%');
                }
                $wxNo = $request->input("wx_no", "");
                if (!empty($wxNo)) {
                    $query->where("wx_no", "like", '%' . $wxNo . '%');
                }
                $query->where("option_type", 10);
            };
            $pagelist = $this->getOrderanalysisInterface()->getWeixinIndex($where);
            if (!empty($pagelist->items())) {
                foreach ($pagelist->items() as $k => $v) {
                    $where2 = function ($query) use ($request, $v) {
                        $start_date = $request->input("start_date", "");
                        if (!empty($start_date)) {
                            $query->where("update_time", ">", strtotime($start_date));
                        }
                        $end_date = $request->input("end_date", "");
                        if (!empty($end_date)) {
                            $query->where("update_time", "<", strtotime($end_date." 23:59:59"));
                        }
                        $wxName = $request->input("wx_name", "");
                        if (!empty($wxName)) {
                            $query->where("wx_name", "like", '%' . $wxName . '%');
                        }
                        $wxNo = $request->input("wx_no", "");
                        if (!empty($wxNo)) {
                            $query->where("wx_no", "like", '%' . $wxNo . '%');
                        }

                        $query->where("option_type", 20);
                        $query->where("wx_no", $v->wx_no);
                    };
                    $v['return_money'] = $this->getOrderanalysisInterface()->getSumSaleMoney($where2);


                    //新粉-->
                    $fenwhere=function($query) use($request,$v){
                        $query->where("option_type",10);
                        $query->where("wx_no",$v->wx_no);
                        $query->where("add_fen_type","新粉");
                    };
                    $v['new_fen'] = $this->getOrderanalysisInterface()->getAddFenType($fenwhere);
                    //老粉-->
                    $oldwhere=function($query) use($request,$v){
                        $query->where("option_type",10);
                        $query->where("wx_no",$v->wx_no);
                        $query->where("add_fen_type","老粉");
                    };
                    $v['old_fen'] = $this->getOrderanalysisInterface()->getAddFenType($oldwhere);
                    //复购-->
                    $buywhere=function($query) use($request,$v){
                        $query->where("option_type",10);
                        $query->where("wx_no",$v->wx_no);
                        $query->where("add_fen_type","复购");
                    };
                    $v['buy_fen'] = $this->getOrderanalysisInterface()->getAddFenType($buywhere);

                    $data[] = $v;






                    $custom = collect(['data' => $data]);
                }
                $custom->merge($pagelist);
            }

            return $pagelist;
        }
        return view("ordersys.orderanalysis.weixin");
    }


    /**
     * 粉丝
     * @param Request $request
     */
    public function fen(Request $request){
        if ($request->getMethod() == 'POST') {
            $where = function ($query) use ($request) {
                $start_date = $request->input("start_date", "");
                if (!empty($start_date)) {
                    $query->where("update_time", ">", strtotime($start_date));
                }
                $end_date = $request->input("end_date", "");
                if (!empty($end_date)) {
                    $query->where("update_time", "<", strtotime($end_date." 23:59:59"));
                }
                $add_fen_type = $request->input("add_fen_type", "");
                if (!empty($add_fen_type)) {
                    $query->where("add_fen_type", $add_fen_type);
                }
                $query->where("option_type", 10);
            };
            $pagelist = $this->getOrderanalysisInterface()->getFenIndex($where);
            if (!empty($pagelist->items())) {
                foreach ($pagelist->items() as $k => $v) {
                    $where2 = function ($query) use ($request, $v) {
                        $start_date = $request->input("start_date", "");
                        if (!empty($start_date)) {
                            $query->where("update_time", ">", strtotime($start_date));
                        }
                        $end_date = $request->input("end_date", "");
                        if (!empty($end_date)) {
                            $query->where("update_time", "<", strtotime($end_date." 23:59:59"));
                        }

                        $add_fen_type = $request->input("add_fen_type", "");
                        if (!empty($add_fen_type)) {
                            $query->where("add_fen_type", $add_fen_type);
                        }
                        $query->where("option_type", 20);
                        $query->where("wx_no", $v->add_fen_type);
                    };
                    $v['return_money'] = $this->getOrderanalysisInterface()->getSumSaleMoney($where2);
                    $data[] = $v;
                    $custom = collect(['data' => $data]);
                }
                $custom->merge($pagelist);
            }

            return $pagelist;
        }
        return view("ordersys.orderanalysis.fen");
    }

    public function month(Request $request){
        if ($request->getMethod() == 'POST') {
            $where = function ($query) use ($request) {
                $start_date = $request->input("start_date", "");
                if (!empty($start_date)) {
                    $query->where("update_time", ">", strtotime($start_date));
                }
                $end_date = $request->input("end_date", "");
                if (!empty($end_date)) {
                    $query->where("update_time", "<", strtotime($end_date." 23:59:59"));
                }
                $add_fen_type = $request->input("add_fen_month", "");
                if (!empty($add_fen_type)) {
                    $query->where("add_fen_month", $add_fen_type);
                }
                if($add_fen_type!=''){
                    $query->where("add_fen_month", $add_fen_type);
                }
                $query->where("option_type", 10);
            };
            $pagelist = $this->getOrderanalysisInterface()->getMonthIndex($where);
            if (!empty($pagelist->items())) {
                foreach ($pagelist->items() as $k => $v) {
                    $where2 = function ($query) use ($request, $v) {
                        $start_date = $request->input("start_date", "");
                        if (!empty($start_date)) {
                            $query->where("update_time", ">", strtotime($start_date));
                        }
                        $end_date = $request->input("end_date", "");
                        if (!empty($end_date)) {
                            $query->where("update_time", "<", strtotime($end_date." 23:59:59"));
                        }

                        $add_fen_type = $request->input("add_fen_month", "");
                        if (!empty($add_fen_type)) {
                            $query->where("add_fen_month", $add_fen_type);
                        }
                        if($add_fen_type!=''){
                            $query->where("add_fen_month", $add_fen_type);
                        }
                        $query->where("option_type", 20);
                        $query->where("wx_no", $v->add_fen_type);
                    };
                    $v['return_money'] = $this->getOrderanalysisInterface()->getSumSaleMoney($where2);
                    $data[] = $v;
                    $custom = collect(['data' => $data]);
                }
                $custom->merge($pagelist);
            }

            return $pagelist;
        }
        return view("ordersys.orderanalysis.month");
    }


    public function province(Request $request){
        if ($request->getMethod() == 'POST') {
            $where = function ($query) use ($request) {
                $start_date = $request->input("start_date", "");
                if (!empty($start_date)) {
                    $query->where($this->getOrdersysOrderLogModel()->table.".update_time", ">", strtotime($start_date));
                }
                $end_date = $request->input("end_date", "");
                if (!empty($end_date)) {
                    $query->where($this->getOrdersysOrderLogModel()->table.".update_time", "<", strtotime($end_date." 23:59:59"));
                }
                $areceiver_province = $request->input("receiver_province", "");
                if (!empty($areceiver_province)) {
                    $query->where("receiver_province", $areceiver_province);
                }
                $query->where("option_type", 10);
                $query->where("order_type", 1);
            };
            $pagelist = $this->getOrderanalysisInterface()->getProvinceIndex($where);

            if (!empty($pagelist->items())) {
                foreach ($pagelist->items() as $k => $v) {
                    $where2 = function ($query) use ($request, $v) {
                        $start_date = $request->input("start_date", "");
                        if (!empty($start_date)) {
                            $query->where($this->getOrdersysOrderLogModel()->table.".update_time", ">", strtotime($start_date));
                        }
                        $end_date = $request->input("end_date", "");
                        if (!empty($end_date)) {
                            $query->where($this->getOrdersysOrderLogModel()->table.".update_time", "<", strtotime($end_date." 23:59:59"));
                        }

                        $areceiver_province = $request->input("receiver_province", "");
//                        if (!empty($areceiver_province)) {
//                            $query->where("receiver_province", $areceiver_province);
//                        }
                        $query->where("receiver_province", $v->receiver_province);
                        $query->where("option_type", 20);
                        $query->where("order_type", 1);
                    };
                    $v['return_money'] = $this->getOrderanalysisInterface()->getCitySumSaleMoney($where2);
                    $data[] = $v;
                    $v['province_name'] = $this->getAreaModel()->newQuery()->where("area_id",$v->receiver_province)->value("title");
                    $data[] = $v;
                    $custom = collect(['data' => $data]);
                }
                $custom->merge($pagelist);
            }
            return $pagelist;
        }
        $areaList=$this->getAreaInterface()->getAreaList(["pid"=>0]);  //区域
            return view("ordersys.orderanalysis.province")->with("areaList",$areaList);
    }
    public function city(Request $request){
        if ($request->getMethod() == 'POST') {
            $where = function ($query) use ($request) {
                $start_date = $request->input("start_date", "");
                if (!empty($start_date)) {
                    $query->where($this->getOrdersysOrderLogModel()->table.".update_time", ">", strtotime($start_date));
                }
                $end_date = $request->input("end_date", "");
                if (!empty($end_date)) {
                    $query->where($this->getOrdersysOrderLogModel()->table.".update_time", "<", strtotime($end_date." 23:59:59"));
                }
                $receiver_city = $request->input("receiver_city", "");
                if (!empty($receiver_city)) {
                    $query->where("receiver_city", $receiver_city);
                }
                $query->where("option_type", 10);
                $query->where("order_type", 1);
            };
            $pagelist = $this->getOrderanalysisInterface()->getCityIndex($where);

            if (!empty($pagelist->items())) {
                foreach ($pagelist->items() as $k => $v) {
                    $where2 = function ($query) use ($request, $v) {
                        $start_date = $request->input("start_date", "");
                        if (!empty($start_date)) {
                            $query->where($this->getOrdersysOrderLogModel()->table.".update_time", ">", strtotime($start_date));
                        }
                        $end_date = $request->input("end_date", "");
                        if (!empty($end_date)) {
                            $query->where($this->getOrdersysOrderLogModel()->table.".update_time", "<", strtotime($end_date." 23:59:59"));
                        }

                        $areceiver_province = $request->input("receiver_province", "");
//                        if (!empty($areceiver_province)) {
//                            $query->where("receiver_province", $areceiver_province);
//                        }
                        $query->where("receiver_city", $v->receiver_city);
                        $query->where("option_type", 20);
                        $query->where("order_type", 1);
                    };
                    $v['return_money'] = $this->getOrderanalysisInterface()->getCitySumSaleMoney($where2);
                    $data[] = $v;
                    $v['city_name'] = $this->getAreaModel()->newQuery()->where("area_id",$v->receiver_city)->value("title");
                    $data[] = $v;
                    $custom = collect(['data' => $data]);
                }
                $custom->merge($pagelist);
            }
            return $pagelist;
        }
        $areaList=$this->getAreaInterface()->getAreaList(["pid"=>0]);  //区域
        return view("ordersys.orderanalysis.city")->with("areaList",$areaList);
    }
    public function goods(Request $request){
        if ($request->getMethod() == 'POST') {

            $where = function ($query) use ($request) {
                $start_date = $request->input("start_date", "");
                if (!empty($start_date)) {
                    $query->where($this->getOrdersysOrderLogModel()->table.".update_time", ">", strtotime($start_date));
                }
                $end_date = $request->input("end_date", "");
                if (!empty($end_date)) {
                    $query->where($this->getOrdersysOrderLogModel()->table.".update_time", "<", strtotime($end_date." 23:59:59"));
                }
                $goods_name = $request->input("goods_name", "");
                if (!empty($goods_name)) {
                    $query->where("goods_name","like",'%'.$goods_name.'%');
                }
                $query->where("option_type", 10);
                $query->where("order_type", 1);
            };
            $pagelist = $this->getOrderanalysisInterface()->getGoodsIndex($where);
            return $pagelist;
        }
        return view("ordersys.orderanalysis.goods");
    }

    /**
     * 日统计
     * @param Request $request
     */
    public function day(Request $request){
        if ($request->getMethod() == 'POST') {

            $where = function ($query) use ($request) {





                $start_date = $request->input("start_date", "");

                if (!empty($start_date)) {
                    $start_time=strtotime($start_date);
                    //$query->where($this->getOrdersysOrderLogModel()->table.".day_time", ">=", strtotime($start_date));
                }
                $end_date = $request->input("end_date", "");
                if (!empty($end_date)) {
                    $end_time=strtotime($end_date." 23:59:59");
                    //$query->where($this->getOrdersysOrderLogModel()->table.".day_time", "<", strtotime($end_date." 23:59:59"));
                }
                if(!empty($start_time)&&!empty($end_time)){
                    $query->whereBetween($this->getOrdersysOrderLogModel()->table.".day_time",[$start_time,$end_time]);
                }

                //$query->where($this->getOrdersysOrderLogModel()->table.".day_time", "<", strtotime($end_date." 23:59:59"));
                $goods_name = $request->input("goods_name", "");
                if (!empty($goods_name)) {
                    $query->where("goods_name","like",'%'.$goods_name.'%');
                }
                $query->where("option_type", 10);
                $query->where("order_type", 1);
            };
            $pagelist = $this->getOrderanalysisInterface()->getDayIndex($where);
            if(!empty($pagelist -> items())){
                foreach($pagelist -> items()  as $k => $v){

                    //新粉-->
                    $fenwhere=function($query) use($request,$v){
                        $query->where("option_type",10);
                        $query->where("day_time",$v->day_time);
                        $query->where("add_fen_type","新粉");
                    };
                    $v['new_fen'] = $this->getOrderanalysisInterface()->getAddFenType($fenwhere);
                    //老粉-->
                    $oldwhere=function($query) use($request,$v){
                        $query->where("option_type",10);
                        $query->where("day_time",$v->day_time);
                        $query->where("add_fen_type","老粉");
                    };
                    $v['old_fen'] = $this->getOrderanalysisInterface()->getAddFenType($oldwhere);
                    //复购-->
                    $buywhere=function($query) use($request,$v){
                        $query->where("option_type",10);
                        $query->where("day_time",$v->day_time);
                        $query->where("add_fen_type","复购");
                    };
                    $v['buy_fen'] = $this->getOrderanalysisInterface()->getAddFenType($buywhere);

                    $data[] = $v;
                    $custom = collect(['data' => $data]) ;
                }
                $custom->merge($pagelist);
            }

            return $pagelist;
        }
        return view("ordersys.orderanalysis.day");
    }
}
